//
//  H_MoreViewController.m
//  Honey
//
//  Created by 贾亚涵 on 16/3/17.
//  Copyright © 2016年 贾亚涵. All rights reserved.
//

#import "H_MoreViewController.h"
#import "H_MoreInfoViewController.h"
#import "H_MoreGameViewController.h"

@interface H_MoreViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
    UIImageView * _moreHeaderBackView;
    UIButton * _photoButton;
    
    UIButton * _inforButton;
    UIButton * _gameButton;
}

@end

@implementation H_MoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //创建本页满上的控件
    [self creatMoreWidget];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    
}

-(void)creatMoreWidget
{
    _moreHeaderBackView = [[UIImageView alloc] initWithFrame:CGRectMake(0 ,0 ,Screen_Width, Screen_Height/3*2)];
    _moreHeaderBackView.backgroundColor = [UIColor yellowColor];
    _moreHeaderBackView.image = [UIImage imageNamed:@"H_MoreHeader3"];
    _moreHeaderBackView.userInteractionEnabled = YES;
    [self.view addSubview:_moreHeaderBackView];
    
    _photoButton = [[UIButton alloc] initWithFrame:CGRectMake(Screen_Width-38, Screen_Height/3*2-35, 30, 30)];
    _photoButton.backgroundColor = [UIColor clearColor];
    [_photoButton setBackgroundImage:[UIImage imageNamed:@"H_PhotoButton"] forState:UIControlStateNormal];
    [_photoButton addTarget:self action:@selector(PhotoButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [_moreHeaderBackView addSubview:_photoButton];
    
    _inforButton = [[UIButton alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(_moreHeaderBackView.frame)+10, (Screen_Width-40)/3, 75)];
    _inforButton.backgroundColor = [UIColor colorWithRed:250/255.0f green:212/255.0f blue:212/255.0f alpha:1];
    _inforButton.layer.cornerRadius = 25.0f;
    [_inforButton setBackgroundImage:[UIImage imageNamed:@"H_MoreInfo"] forState:UIControlStateNormal];
    [_inforButton addTarget:self action:@selector(MoreInforBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_inforButton];
    
    _gameButton = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_inforButton.frame)+10, CGRectGetMaxY(_moreHeaderBackView.frame)+10, (Screen_Width-40)/3, 75 )];
    _gameButton.backgroundColor = [UIColor colorWithRed:250/255.0f green:212/255.0f blue:212/255.0f alpha:1];
    _gameButton.layer.cornerRadius = 25.0f;
    [_gameButton setBackgroundImage:[UIImage imageNamed:@"H_MoreGame"] forState:UIControlStateNormal];
    [_gameButton addTarget:self action:@selector(MoreGameBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_gameButton];
}

-(void)PhotoButtonClicked:(id)sender
{
    if (sender && [sender isKindOfClass:[UIButton class]])
    {
        UIAlertController * sheetView = [UIAlertController alertControllerWithTitle:@"选择操作" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        
        UIAlertAction * cancleAction = [UIAlertAction actionWithTitle:@"取消操作" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction * cameraAction = [UIAlertAction actionWithTitle:@"拍摄照片" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
        {
            [self takePhoto];
        }];
        
        UIAlertAction * photoAction = [UIAlertAction actionWithTitle:@"从照片中选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            [self localPhoto];
        }];
        
        [sheetView addAction:cancleAction];
        [sheetView addAction:cameraAction];
        [sheetView addAction:photoAction];
        
        [self presentViewController:sheetView animated:YES completion:nil];


    }
}

-(void)takePhoto
{
    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
    
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    
    picker.delegate = self;
    
    picker.allowsEditing = YES;
    
    [self presentViewController:picker animated:YES completion:^{
        
    }];
}

-(void)localPhoto
{
    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
    
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    
    picker.delegate = self;
    
    [self presentViewController:picker animated:YES completion:^{
        
    }];
    
}

#pragma mark- UIImagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
    
    UIImage * image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    
    if (image)
    {
        _moreHeaderBackView.image = image;
    }
}

-(void)MoreInforBtnClicked:(id)sender
{
    if (sender && [sender isKindOfClass:[UIButton class]])
    {
        H_MoreInfoViewController * infor = [H_MoreInfoViewController new];
        [self.navigationController pushViewController:infor animated:YES];
    }
}

-(void)MoreGameBtnClicked:(id)sender
{
    if (sender && [sender isKindOfClass:[UIButton class]])
    {
        H_MoreGameViewController * game = [H_MoreGameViewController new];
        [self.navigationController pushViewController:game animated:YES];
    }
}

@end
